// views/index/shop/translationcopy/ordermessage/index.js
var util = require('../../../../../../utils/util');
var BASE_URL = getApp().globalData.BASE_URL
import Notify from '../../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    name: '',
    p_name: '',
    tel: '',
    wechat: '',
    email: "",
    price: 125,
    dongji_gao: '',
    difficult: ''
  },

  change_num: function (e) {
    this.setData({
      num: e.currentTarget.dataset.num
    })
    this.getfile()
    if (e.currentTarget.dataset.num == 1) {
      this.setData({
        price: 125
      })
    }
    if (e.currentTarget.dataset.num == 2) {
      this.setData({
        price: 250
      })
    }
    if (e.currentTarget.dataset.num == 3) {
      this.setData({
        price: 375
      })
    }
    if (e.currentTarget.dataset.num == 4) {
      this.setData({
        price: 500
      })
    }
    if (e.currentTarget.dataset.num == 5) {
      this.setData({
        price: 499
      })
    }
  },


  //上传文件
  upfile: function () {
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    wx.chooseMessageFile({
      count: 100,
      success(res) {
        wx.showLoading({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
        })
        res.tempFiles.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
          console.log(item)
        })
        const tempFilePaths = res.tempFiles
        console.log(res, tempFilePaths)
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: `${BASE_URL}/fanyi/dongjiupload`, //仅为示例，非真实的接口地址
            filePath: tempFilePaths[i].path,
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              uid: userinfo.id,
              file: tempFilePaths[i].name,
              type: 'defuzuowen'
            },
            success(res) {
              _this.getfile()
              if (res.statusCode == 200) {
                wx.hideLoading()
                wx.showToast({
                  title: "上传成功",
                  icon: "none",
                  duration: 1500
                })
              }
              var data = JSON.parse(res.data)
              console.log(data)
              if (data.status == 4) {
                wx.showToast({
                  title: `上传失败,${data.error}`,
                  icon: "none",
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  },
  //删除文件
  del_file: function (e) {
    var userinfo = wx.getStorageSync("USER")
    var _this = this
    wx.showModal({
      title: '提示',
      content: '你确定要删除吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/fanyi/infodelete`,
            method: "GET",
            data: {
              id: e.currentTarget.dataset.id,
              type: 'xiaochengxv'
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {
              console.log(res)
              _this.getfile()
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },




  //立即办理
  placeorder: function () {
    console.log(11)
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    if (!_this.ifhanzi(_this.data.name)) {
      Notify('姓名输入错误');
    }
    if (!_this.ifenglish(_this.data.p_name)) {
      Notify('姓名拼音输入错误');
    }
    if (!_this.codeVerification(_this.data.tel)) {
      Notify('请输入正确的手机号');
    }
    if (_this.data.difficult == '') {
      Notify('内容不能为空');
    }

    if (_this.ifhanzi(_this.data.name) && _this.ifenglish(_this.data.p_name) && _this.codeVerification(_this.data.tel) && !_this.data.difficult == '') {
      if (_this.data.num == 5) {
        wx.request({
          url: `${BASE_URL}/fanyi/zuowen/cateid/2.html`,
          method: "POST",
          data: {
            name: _this.data.name,
            email: _this.data.email,
            zuowen_num: _this.data.num,
            p_name: _this.data.p_name,
            tel: _this.data.tel,
            wechat: _this.data.wechat,
            difficult: _this.data.difficult,
            uid: userinfo.id,
            type: 'xiaochengxv'
          },
          header: {
            'Content-Type': 'application/json' //GET方式是这个
          },
          success(res) {
            console.log(res)
            wx.navigateTo({
              url: `../zuowenorderdetail/index?id=${res.data.id}`
            })
          }
        })
      } else {
        wx.request({
          url: `${BASE_URL}/fanyi/zuowen/cateid/1.html`,
          method: "POST",
          data: {
            name: _this.data.name,
            email: _this.data.email,
            zuowen_num: _this.data.num,
            p_name: _this.data.p_name,
            tel: _this.data.tel,
            wechat: _this.data.wechat,
            difficult: _this.data.difficult,
            uid: userinfo.id,
            type: 'xiaochengxv'
          },
          header: {
            'Content-Type': 'application/json' //GET方式是这个
          },
          success(res) {
            console.log(res)
            wx.navigateTo({
              url: `../zuowenorderdetail/index?id=${res.data.id}`
            })
          }
        })
      }



    }
  },















  getfile: function () {

    var userinfo = wx.getStorageSync("USER")
    var _this = this
    if (_this.data.num == 5) {
      wx.request({
        url: `${BASE_URL}/fanyi/zuowen/cateid/2.html`,
        method: "GET",
        data: {
          uid: userinfo.id,
          type: 'xiaochengxv'
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          _this.setData({
            dongji_gao: res.data.cailiao,
          })
        }
      })
    } else {
      wx.request({
        url: `${BASE_URL}/fanyi/zuowen/cateid/1.html`,
        method: "GET",
        data: {
          uid: userinfo.id,
          type: 'xiaochengxv'
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          _this.setData({
            dongji_gao: res.data.cailiao,
          })
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getfile()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userinfo = wx.getStorageSync("USER")
    this.setData({
      email: userinfo.email
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },









  // 时间戳转换
  changeTime(time) {
    if (time) {
      var oDate = new Date(time * 1),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oTime = oYear + "/" + this.getBz(oMonth) + "/" + this.getBz(oDay)
      return oTime;
    } else {
      return "";
    }
  },
  // 补0
  getBz(num) {
    if (parseInt(num) < 10) {
      num = "0" + num;
    }
    return num;
  },


})